package com.gorkr.hot100.medium;

import org.junit.jupiter.api.Test;

/**
 * 打家劫舍
 *
 * @author 张心宇 <zhangxinyu20@kuaishou.com>
 * Created on 2023-01-06
 */
public class M198HouseRober {
    public int rob(int[] nums) {
        if(nums.length==1){
            return nums[0];
        } else if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int[] dp = new int[nums.length];
        // todo 处理边界
        // base case
        dp[0] = nums[0];
        dp[1] = Math.max(nums[0], nums[1]);

        for (int i = 2; i <nums.length; i++) {
            dp[i] = Math.max(dp[i-1], dp[i-2]+nums[i]);
        }
        return Math.max(dp[nums.length-1], dp[nums.length-2]);
    }

    @Test
    void test(){
        System.out.println(rob(new int[] {2,7,9,3,1}));
    }
}
